package com.nmsl.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.nmsl.system.domain.JinDepartmentsDTO;
import com.nmsl.system.mapper.JinEmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nmsl.system.mapper.JinDepartmentsMapper;
import com.nmsl.system.domain.JinDepartments;
import com.nmsl.system.service.IJinDepartmentsService;

/**
 * 科室Service业务层处理
 *
 * @author jin
 * @date 2025-02-19
 */
@Service
public class JinDepartmentsServiceImpl implements IJinDepartmentsService {
    @Autowired
    private JinDepartmentsMapper jinDepartmentsMapper;

    /**
     * 查询科室
     *
     * @param departmentId 科室主键
     * @return 科室
     */
    @Override
    public JinDepartments selectJinDepartmentsByDepartmentId(Long departmentId) {
        return jinDepartmentsMapper.selectJinDepartmentsByDepartmentId(departmentId);
    }

    @Autowired
    JinEmployeeMapper jinEmployeeMapper;

    /**
     * 查询科室列表
     *
     * @param jinDepartments 科室
     * @return 科室
     */
    @Override
    public List<JinDepartments> selectJinDepartmentsList(JinDepartments jinDepartments) {
        List<JinDepartments> jinDepartments1 = jinDepartmentsMapper.selectJinDepartmentsList(jinDepartments);
        for (JinDepartments jinDepartment : jinDepartments1) {
            if (jinDepartment.getCreator() != null && jinEmployeeMapper.selectJinEmployeeById(jinDepartment.getCreator()) != null) {
                jinDepartment.setCreatorName(jinEmployeeMapper.selectJinEmployeeById(jinDepartment.getCreator()).getName());
            }
        }
        return jinDepartments1;
    }

    public List<JinDepartments> selectJinDepartmentsTree() {
        // 查询所有部门列表
        List<JinDepartments> jinDepartments = selectJinDepartmentsList(null);

        // 将部门列表按照父部门ID进行分组
        Map<String, List<JinDepartments>> groupedByParentId = jinDepartments.stream()
                .collect(Collectors.groupingBy(department -> Objects.toString(department.getParentId(), "0")));
        // 构建部门树
        return buildTree(groupedByParentId, "0");
    }

    private List<JinDepartments> buildTree(Map<String, List<JinDepartments>> groupedByParentId, String parentId) {
        // 根据parentId获取对应的子部门列表，如果没有则返回空列表
        return groupedByParentId.getOrDefault(parentId, Collections.emptyList()).stream()
                // 遍历子部门列表
                .map(department -> {
                    // 递归调用buildTree方法，获取子部门的子部门列表
                    List<JinDepartments> children = buildTree(groupedByParentId, Objects.toString(department.getDepartmentId(), "0"));
                    // 将JinDepartments列表转换为JinDepartmentsDTO列表
                    List<JinDepartmentsDTO> dtoList = children.stream().map(item -> {
                        JinDepartmentsDTO dto = new JinDepartmentsDTO();
                        dto.setDisabled(false);
                        dto.setId(item.getDepartmentId());
                        dto.setLabel(item.getDepartmentName());
                        dto.setChildren(item.getChildren());
                        // 设置其他属性...
                        return dto;
                    }).collect(Collectors.toList());
                    // 设置子部门列表
                    department.setChildren(dtoList);
                    return department;
                })
                // 将子部门列表转换为List
                .collect(Collectors.toList());
    }

    /**
     * 新增科室
     *
     * @param jinDepartments 科室
     * @return 结果
     */
    @Override
    public int insertJinDepartments(JinDepartments jinDepartments) {
        return jinDepartmentsMapper.insertJinDepartments(jinDepartments);
    }

    /**
     * 修改科室
     *
     * @param jinDepartments 科室
     * @return 结果
     */
    @Override
    public int updateJinDepartments(JinDepartments jinDepartments) {
        return jinDepartmentsMapper.updateJinDepartments(jinDepartments);
    }

    /**
     * 批量删除科室
     *
     * @param departmentIds 需要删除的科室主键
     * @return 结果
     */
    @Override
    public int deleteJinDepartmentsByDepartmentIds(Long[] departmentIds) {
        return jinDepartmentsMapper.deleteJinDepartmentsByDepartmentIds(departmentIds);
    }

    /**
     * 删除科室信息
     *
     * @param departmentId 科室主键
     * @return 结果
     */
    @Override
    public int deleteJinDepartmentsByDepartmentId(Long departmentId) {
        return jinDepartmentsMapper.deleteJinDepartmentsByDepartmentId(departmentId);
    }
}
